﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class PlayerMove : NetworkBehaviour {
	public GameObject bulletPrefab;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (!isLocalPlayer)
			return;

		var x = Input.GetAxis ("Horizontal");
		var z = Input.GetAxis ("Vertical");
		transform.Translate (x * 0.1f, 0, z * 0.1f);

		if (Input.GetKeyDown (KeyCode.Space)) {
			CmdFire ();
		}

	}

	public override void OnStartLocalPlayer ()
	{
		base.OnStartLocalPlayer ();
		GetComponent<Renderer> ().material.color = Color.red;
	}
	[Command]
	public void CmdFire()
	{
		var bullet = Instantiate (bulletPrefab, transform.position + transform.forward, Quaternion.identity) as GameObject;
		bullet.GetComponent<Rigidbody> ().velocity = transform.forward * 4;
		NetworkServer.Spawn (bullet);
		Destroy (bullet.gameObject, 4);

	}
}
